#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2014-08-22 01:14:23
# @Author  : shitao.tommy (hero007asd@gmail.com)
# @Link    : http://example.org
# @Version : $Id$

from framework.db.basedb import BaseDb
from tornado import gen
from lib import log,libtime


class Employee(BaseDb):

    @gen.coroutine
    def new(self,emp_name,age,dorm_id,img,entry_time):
        '''新增保洁职工'''
        resp = ''
        ins_sql = '''INSERT INTO tp_dorm_employee(Fname,Fimg,Fdorm_id,Fage,Fentry_time,Fcreate_time) VALUES(%s,%s,%s,%s,%s,UNIX_TIMESTAMP())'''
        try:
            ret = yield self.upd_or_ins(ins_sql,(emp_name,img,dorm_id,age,entry_time))
            if ret > 0:
                resp = '1'
            else: resp = '0'
        except Exception, e:
            import traceback
            e = traceback.format_exc()
            log.log_exception(self.__class__.__name__, e)
            resp = '-1'
        raise gen.Return(resp)


    @gen.coroutine
    def list(self, page=0, offset=20):
        '''保洁职工列表'''
        resp = ''
        sel_sql = '''SELECT a.Fid,a.Fname,a.Fimg,a.Fdorm_id,a.Fage,a.Fentry_time,b.Fdorm_name
                     FROM tp_dorm_employee a
                    LEFT JOIN tp_dorm_base b
                    ON a.Fdorm_id = b.Fid
                    WHERE a.Fstatus = 1 ORDER BY a.Fid DESC
                    LIMIT %s,%s'''
        try:
            ret = yield self.execute(sel_sql,(int(page), int(offset)))
            resp = []
            for i in ret:
                resp.append({'id':i[0],'name':i[1],'img':i[2],'dormId':i[3],'age':i[4], 'entryName':i[5], 'dormName':i[6]})
        except Exception, e:
            import traceback
            e = traceback.format_exc()
            log.log_exception(self.__class__.__name__, e)
            resp = '-1'
        raise gen.Return(resp)


    @gen.coroutine
    def mylist(self,student_id, page=0, offset=20):
        '''手机端保洁职工列表'''
        resp = ''
        sel_sql = '''SELECT distinct(a.Fid),a.Fname,a.Fimg,a.Fdorm_id,a.Fage,a.Fentry_time,b.Fdorm_name,c.Fis_like
                     FROM tp_dorm_employee a
                    LEFT JOIN tp_dorm_base b
                    ON a.Fdorm_id = b.Fid
                    LEFT JOIN tp_dorm_like c
                    ON a.Fid = c.Femployee_id
                    AND c.Fstudent_id = %s
                    WHERE a.Fstatus = 1
                    LIMIT %s,%s'''
        try:
            ret = yield self.execute(sel_sql,(student_id, int(page), int(offset)))
            resp = []
            for i in ret:
                is_like = 0
                if i[7] == 1:
                    is_like = 1
                resp.append({'id':i[0],'name':i[1],'img':i[2],'dormId':i[3]
                    ,'age':i[4], 'entryName':i[5], 'dormName':i[6], 'isLike':is_like})
        except Exception, e:
            import traceback
            e = traceback.format_exc()
            log.log_exception(self.__class__.__name__, e)
            resp = '-1'
        raise gen.Return(resp)

    @gen.coroutine
    def upd(self,emp_id, emp_name,age,dorm_id,img,entry_time):
        '''更新保洁职工'''
        resp = ''
        upd_sql = '''UPDATE tp_dorm_employee SET 
                    Fname = %s
                    ,Fimg =%s
                    ,Fdorm_id = %s
                    ,Fage = %s
                    ,Fentry_time = %s
                    WHERE Fid = %s'''
        try:
            ret = yield self.upd_or_ins(upd_sql,(emp_name,img,dorm_id,age,entry_time,emp_id))
            if ret > 0:
                resp = '1'
            else: resp = '0'
        except Exception, e:
            import traceback
            e = traceback.format_exc()
            log.log_exception(self.__class__.__name__, e)
            resp = '-1'
        raise gen.Return(resp)

    @gen.coroutine
    def delete(self, emp_id):
        '''删除保洁职工'''
        resp = ''
        upd_sql = '''UPDATE tp_dorm_employee SET 
                    Fstatus = 0
                    WHERE Fid = %s'''
        try:
            ret = yield self.upd_or_ins(upd_sql,(emp_id,))
            if ret > 0:
                resp = '1'
            else: resp = '0'
        except Exception, e:
            import traceback
            e = traceback.format_exc()
            log.log_exception(self.__class__.__name__, e)
            resp = '-1'
        raise gen.Return(resp)